﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using medicine.BaseComponents.Groups;
using Microsoft.Office.Interop.Owc11;

namespace medicine.General
{
    public class GridUtil
    {
        public static void FillGrid(AxMicrosoft.Office.Interop.Owc11.AxPivotTable axPivotTable1, AxMicrosoft.Office.Interop.Owc11.AxChartSpace chartSpace, string SelectStatment, List<string> Rows, List<PeriodClass> Periods, List<UnitsClass> Measures)
        {

            
            //axPivotTable1.cap
           
            string ConnectionSTR = DAL.DALUtilities.GetConnectionString();
            axPivotTable1.ConnectionString = ConnectionSTR;
            axPivotTable1.CommandText = SelectStatment; 

            PivotView view = axPivotTable1.ActiveView;
            PivotFieldSets fsets = axPivotTable1.ActiveView.FieldSets;
            
            for (int i = Rows.Count-1; i >=0; i--)
            {
                
                view.RowAxis.InsertFieldSet(fsets[Rows[i]],false, false);
                view.RowAxis.View.ExpandDetails = PivotTableExpandEnum.plExpandNever;
                fsets[Rows[i]].Fields[0].Caption = Rows[i].Substring(3);
             
             }

            foreach (UnitsClass item in Measures)
            {
                PivotTotal newtotal = view.AddTotal(item.UnitName, view.FieldSets[item.FieldName ].Fields[0], PivotTotalFunctionEnum.plFunctionSum);
                view.DataAxis.InsertTotal(newtotal);
                //switch (item)
                //{
                //    case "units":
                //        PivotTotal newtotal = view.AddTotal("Units", view.FieldSets["curUnits"].Fields[0], PivotTotalFunctionEnum.plFunctionSum);
                //        view.DataAxis.InsertTotal(newtotal);
                        
                // break;
                //    case "J.D": //view.DataAxis.InsertFieldSet(axPivotTable1.ActiveView.FieldSets["curValue"]);
                // PivotTotal newtotal2 = view.AddTotal("JOD", view.FieldSets["curValue"].Fields[0], PivotTotalFunctionEnum.plFunctionSum );

                // view.DataAxis.InsertTotal(newtotal2);
                // break;
                  
                       
                //}
            }

           
            PivotFieldSet SDate = axPivotTable1.ActiveView.FieldSets["SDate"];
            view.ColumnAxis.InsertFieldSet(SDate, false, false);
            PivotField PeriodPF = SDate.AddCustomGroupField("Periods", "Periods");
            PeriodPF.Subtotals[0] = false;
            
            //Anas: PeriodPF.SubtotalBackColor = "red";

            foreach (var item in Periods)
            {
                IList<object> PItem = new List<object>();
                foreach (PivotMember Memitem in (SDate).Members)
                {
                    if ((Memitem).Caption=="Other")
                    {
                        //continue;
                        
                    }
                    foreach (PivotMember ChildMembersitem in Memitem.ChildMembers)
                    {
                        DateTime ChildMembersitemDate = new DateTime(int.Parse(ChildMembersitem.Caption.Substring(0, 4)), int.Parse(ChildMembersitem.Caption.Substring(4)), 1);
                        if (ChildMembersitemDate >= item.FromDate && ChildMembersitemDate <= item.ToDate)
                        {
                            PItem.Add(ChildMembersitem.Caption);
                        }

                    }
                      
                }
                if (PItem.Count>0)
                {

                    PeriodPF.AddCustomGroupMember("All", PItem.ToArray(), item.Display);
                    
                }
            }
            (SDate).Fields[1].IsIncluded = false;
            

       
            axPivotTable1.AllowDetails = false;
            ////axPivotTable1.ActiveView;
            ////view.DataAxis.InsertFieldSet(view.FieldSets["Price"],);
            //AxChartSpace2. = axPivotTable1.DataSource;
            
           //PivotView pview = axPivotTable1.ActiveView;
           //ChChart chart;
           //chartSpace.DataSource = (msdatasrc.DataSource)axPivotTable1.GetOcx();
           // chartSpace.DisplayFieldList = true;
           // if (chartSpace.Charts.Count == 0)
           //     chart = chartSpace.Charts.Add(0);
           // else
           //     chart = chartSpace.Charts[0];
           // chart.HasLegend = true;
           // //chart.HasTitle = true;
           // Anas: chart.Title.Caption = pview.TitleBar.Caption;

         
            //chart.SetData(ChartDimensionsEnum.chDimCategories, 0,ChartPivotDataReferenceEnum.chPivotRows);


        }
    }
}
